package com.ham.mud.commands;

import com.ham.mud.ServerConnection;
import com.ham.mud.TargetableService;
import com.ham.mud.characters.mobs.Mob;
import com.ham.mud.characters.npcs.VendorNPC;
import com.ham.mud.characters.player.Player;
import com.ham.mud.items.Item;
import com.ham.mud.rooms.Room;
import com.ham.mud.rooms.RoomService;

import java.util.List;

/**
 * Created by hlucas on Jul 19, 2011 at 10:26:43 AM
 */
public class SellCommand extends PlayerCommand {
    @Override
    public void execute(ServerConnection connection, String[] params) {
        Room room = RoomService.getRoom(connection);
        List<Mob> mobs = room.getMobs();
        VendorNPC npc = null;
        for(Mob mob : mobs) {
            if(mob instanceof VendorNPC) {
                npc = (VendorNPC) mob;
                break;
            }
        }

        if(npc == null) {
            connection.printSolo("This is not a shop.");
            return;
        }

        if(params.length == 0) {
            connection.printSolo("Sell what?");
            return;
        }

        Player player = connection.getPlayer();
        List<Item> items = player.getInventory();
        Item item = (Item) TargetableService.findTarget(params[0], items);
        if(item == null ) {
            connection.printSolo("You do not have that item to sell.");
            return;
        }

        int priceHalf = item.getPrice() / 2;
        player.incrementGold(priceHalf);
        player.removeItem(item);
        connection.printSolo("You sell " + item.getName() + " to " + npc.getName() + " for " + priceHalf + " gold.");
    }

    @Override
    protected String getCommandName() {
        return "sell";
    }
}
